package com.m1905hd.user.shiro.filter;

import com.m1905hd.user.resource.Global;
import com.m1905hd.user.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.PathMatchingFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/3/31
 */
public class SysUserFilter extends PathMatchingFilter {

    UserService userService;

    @Override
    protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        String username = (String) SecurityUtils.getSubject().getPrincipal();
        request.setAttribute(Global.CURRENT_USER, this.userService.findByUserName(username));
        return true;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
